<html>
<head>
    <title>吉垚物业后台管理系统</title>
    <meta charset="utf-8">
    <link rel="shortcut icon" th:href="./images/favicon.ico" type="image/x-icon">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <!-- 引入css -->
    <link rel="stylesheet" href="./css/plugins/font-awesome-4.7.0/css/font-awesome.min.css"  media="all">
    <link rel="stylesheet" href="./lib/layui-v2.3.0/css/layui.css"  media="all">
    <link rel="stylesheet" href="./css/main.css"  media="all">
    <link rel="stylesheet" href="./css/bootstrap.min.css">
</head>
<body>
<div class="">
    <form action="#">
        <div class="layui-form-item">
            <label class="layui-form-label">名称</label>
            <div class="layui-input-inline">
                <input type="text" name="name" id="name" lay-verify="title" autocomplete="off"  class="layui-input">
            </div>
        </div>
        <div class="layui-form-item" id="logo">
            <label class="layui-form-label">LOGO</label>
            <div class="layui-input-inline">
                <input type="file" name="img" id="img" lay-verify="title" autocomplete="off"class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">描述</label>
            <div class="layui-input-inline">
                <textarea type="text" name="describes" style="height: 100px" id="describes" lay-verify="title" autocomplete="off" placeholder="请输入文章内容" class="layui-input"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">电话</label>
            <div class="layui-input-inline">
                <input type="text" name="telephone" id="telephone" lay-verify="title" autocomplete="off"  class="layui-input">
            </div>
        </div>
        <div >
            <label class="layui-form-label">选择框</label>
            <div class="layui-input-inline">
                <select name="interest" id="types" lay-filter="aihao">
                </select>
            </div>
        </div>
        <hr>
        <div class="layui-form-item timo-finally">
            <button class="layui-btn ajax-submit" onclick="addCoop()"><i class="fa fa-check-circle"></i> 保存</button>
            <button class="layui-btn btn-secondary close-popup" id="close"><i class="fa fa-times-circle"></i> 关闭</button>
        </div>
    </form>
</div>
<script src="./lib/layui-v2.3.0/layui.js" charset="utf-8"></script>
<script src="./js/main.js" charset="utf-8"></script>
<script src="js/plugins/jquery-3.3.1.min.js"></script>
</body>
<script type="text/javascript">
    $.ajax({
        url:'/admin/getTypeByBelong.do?belong=coop',
        headers:{
            accessToken:localStorage.getItem('accessToken')
        },
        success:function (data) {
            if (data.resMsg==undefined)
                data=JSON.parse(data);
            if (data.code==401||data.code==402){
                localStorage.clear();
                sessionStorage.clear();
                top.location.href='notlogin.html';
                this.close();
            }
            console.log(data.result);
            for (var i=0;i<data.result.length;i++) {
                    $('#types').append('<option value="'+data.result[i]['id']+'">'+data.result[i]['name']+'</option>')
            }
        }
    })

    function addCoop() {
        var coopname=$('#name').val();
        var describes=$('#describes').val();
        var telephone=$('#telephone').val();
        var type=$('#types').val();
        // var userid=sessionStorage.getItem('userid');
        var img = document.getElementById("img").files[0];

        if (coopname==''){
            alert('标题不能为空');
            return;
        }
        var formData = new FormData();
        formData.append('name',coopname);
        formData.append('describes',describes);
        formData.append('telephone',telephone);
        formData.append('type',type);
        // formData.append('userid',userid);
        formData.append('img',img);
        $.ajax({
            url:'/admin/addCoop.do',
            headers:{
                accessToken:localStorage.getItem('accessToken')
            },
            type: "post",
            data: formData,
            contentType: false,
            processData: false,
            success:function (data) {
                if (data.resMsg==undefined)
                    data=JSON.parse(data);
                if (data.code==401||data.code==402){
                    localStorage.clear();
                    sessionStorage.clear();
                    top.location.href='notlogin.html';
                    this.close();
                }
                if (data.resMsg!='添加失败'){
                    top.location.reload();
                    this.close();
                }else{
                    alert('添加失败');
                }
            }
        })
    }

</script>
</html>